package top.jybill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.jybill.domain.MyPageInfo;
import top.jybill.domain.Permission;
import top.jybill.domain.ResponseData;
import top.jybill.service.IPermissionService;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
  @Autowired
  private ResponseData res;
  @Autowired
  private IPermissionService permissionServicel;

  /**
   * 根据角色id查询所有的permission
   * @param rid
   * @return
   */
  @RequestMapping("/findPermissionByRid")
  @ResponseBody
  public ResponseData findPermissionByRid(String rid) {
    res.clean();

    List<Permission> permissions = permissionServicel.findPermissionsByrid(rid);
    if (permissions == null) {
      res.metaPut("status", 201);
      res.metaPut("msg", "没有分配任何权限");
      return res;
    }
    res.metaPut("status", 200);
    res.metaPut("msg", "查询成功");
    res.dataPut("permissions", permissions);
    return res;
  }

  /**
   * 查询所有的权限
   * @return
   */
  @RequestMapping("/findAllPermissions")
  @ResponseBody
  public ResponseData findAllPermissions() {
    res.clean();
    res.dataPut("permissions", permissionServicel.findAllPermissions());
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    System.out.println(res);
    return res;
  }

  /**
   * 分页查询
   * @param page
   * @return
   */
  @RequestMapping(value = "/findPermissionsByPage", method = RequestMethod.GET)
  @ResponseBody
  public ResponseData findPermissionsByPage(MyPageInfo page) {
    res.clean();

    // size、num参数校验
    if (page.getPageNum() < 1 || page.getPageNum() < 1) {
      res.metaPut("status", 401);
      res.metaPut("msg", "参数错误");
      return res;
    }

    res.dataPut("page", permissionServicel.findPermissionsByPage(page));
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 添加权限
   * @param permission
   * @return
   */
  @RequestMapping(value = "/addPermission", method = RequestMethod.POST)
  @ResponseBody
  public ResponseData addPermission(@RequestBody Permission permission) {
    res.clean();
    permissionServicel.addPermission(permission);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 更新权限
   * @param permission
   * @return
   */
  @RequestMapping(value = "/updatePermission", method = RequestMethod.POST)
  @ResponseBody
  public ResponseData updatePermission(@RequestBody Permission permission) {
    res.clean();
    permissionServicel.updatePermission(permission);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 删除权限
   * @param pid
   * @return
   */
  @RequestMapping(value = "/delPermission/{pid}", method = RequestMethod.DELETE)
  @ResponseBody
  public ResponseData delPermission(@PathVariable("pid") String pid) {
    res.clean();
    permissionServicel.delPermission(pid);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  @RequestMapping(value = "/findPermissionByPid", method = RequestMethod.GET)
  @ResponseBody
  public ResponseData findPermissionByPid(String pid) {
    res.clean();
    res.dataPut("permission", permissionServicel.findPermissionByPid(pid));
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }
}
